package com.xkstream.client.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.BaseRecycleViewAdapter;
import com.xkstream.client.entity.kt.BlogInfo;
import com.xkstream.client.presenter.kt.SendTalkContract;
import com.xkstream.client.utils.AsyncImage;
import com.xkstream.client.utils.DeviceUtils;
import com.xkstream.client.videothumbloader.config.DisplayConfig;
import com.xkstream.client.videothumbloader.core.VideoThumbLoader;
import com.xkstream.client.view.CircleImageView;
import com.xkstream.client.view.FlowLayout;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017-9-27.
 */

public class MyZoneAdapter extends BaseRecycleViewAdapter {

    private List<BlogInfo> mDatas;
    private int width;
    private WeakReference<Context> mContextRef;

    public MyZoneAdapter(Context mContext, List<BlogInfo> mDatas) {
        super(mContext);
        this.mDatas = mDatas;
        this.mContextRef = new WeakReference<Context>(mContext);
        width = DeviceUtils.deviceWidth(mContextRef.get()) - DeviceUtils.dp2px(mContextRef.get(), 48);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_myzone_item, parent, false);
        ItemHolder holder = new ItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindItem((ItemHolder) holder, position);
    }

    private void onBindItem(ItemHolder holder, int pos) {
        BlogInfo data = mDatas.get(pos);
        holder.tvAgree.setText(data.getUpvote() + "");
        holder.tvComment.setText(data.getComment() + "");
        holder.tvTime.setText(data.getCreate_date());
        holder.tvNickname.setText(Application.getUser().getExt_info().getNickname());
        AsyncImage.loadPhoto(mContextRef.get(),Application.getUser().getExt_info().getHead_icon(),holder.ivHeader);
        if (TextUtils.isEmpty(data.getContent())) {
            holder.tvContent.setVisibility(View.GONE);
        } else {
            holder.tvContent.setVisibility(View.VISIBLE);
        }
        holder.tvContent.setText(data.getContent());

        holder.tvContent.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        if (data.getImages() != null) {
            showImage(data.getImages(), holder.llImage);
        }

        if (data.getVideos() != null) {
            showVideos(data.getVideos(), holder.llImage);
        }

        if (!TextUtils.isEmpty(data.getUpvote_id())) {
            holder.tvAgree.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_agreed
                    , 0, 0, 0);
            holder.tvAgree.setEnabled(false);
        } else {
            holder.tvAgree.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_agree
                    , 0, 0, 0);
            holder.tvAgree.setEnabled(true);
        }

        if (data.getImages() != null && data.getImages().length > 0
                || data.getVideos() != null && data.getVideos().length > 0) {
            holder.line.setVisibility(View.GONE);
            holder.llImage.setVisibility(View.VISIBLE);
        } else {
            holder.line.setVisibility(View.VISIBLE);
            holder.llImage.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener((v) -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, pos);
            }
        });
        holder.tvAgree.setOnClickListener((v) -> {

            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, pos);
            }
        });
        holder.tvDelete.setOnClickListener((v)->{
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, pos);
            }
        });
    }

    private void showVideos(String[] videos, FlowLayout parent) {
        parent.removeAllViews();
        int imgSize = (int) ((width - DeviceUtils.dp2px(mContextRef.get(), 10)) / 1.5);
        for (String url : videos) {
            View view = mInflater.inflate(R.layout.zone_image_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            FlowLayout.LayoutParams params = new RecyclerView.LayoutParams(imgSize, imgSize);
            //AsyncImage.loadPhoto(mContextRef.get(), url, imageView);
            DisplayConfig config = new DisplayConfig();
            config.loadingResId = R.drawable.default_img;
            config.failedResId = R.drawable.default_img;
            VideoThumbLoader.getInstance().loadThumb(imageView, url, config,
                    (v, b, u) -> {
                        int height = view.getHeight();
                        int width = height * b.getWidth() / b.getHeight();
                        ViewGroup.LayoutParams rlParams = view.getLayoutParams();
                        rlParams.width = width;
                        rlParams.height = height;
                        view.setLayoutParams(rlParams);
                    });

            ImageView play = new ImageView(mContextRef.get());
            play.setImageResource(R.drawable.icon_play);
            RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(imageView.getLayoutParams());
            rlParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            rlParams.width = DeviceUtils.dp2px(mContextRef.get(), 30);
            rlParams.height = DeviceUtils.dp2px(mContextRef.get(), 30);
            ((RelativeLayout) view).addView(play, rlParams);

            int padding = DeviceUtils.dp2px(mContextRef.get(), 5);
            view.setPadding(padding, padding, padding, padding);
            parent.addView(view, params);
        }

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvNickname)
        TextView tvNickname;
        @BindView(R.id.ivHeader)
        CircleImageView ivHeader;
        @BindView(R.id.tvTime)
        TextView tvTime;
        @BindView(R.id.tvContent)
        TextView tvContent;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.llImage)
        FlowLayout llImage;
        @BindView(R.id.tvComment)
        TextView tvComment;
        @BindView(R.id.tvAgree)
        TextView tvAgree;
        @BindView(R.id.tvDelete)
        TextView tvDelete;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private void showImage(String[] urls, FlowLayout parent) {
        parent.removeAllViews();
        switch (urls.length) {
            case 0:
                break;
            case 1:
                showImageOne(urls, parent);
                break;
            case 2:
                showImageTwo(urls, parent);
                break;
            case 3:
                showImageThreeMore(urls, parent);
                break;
            case 4:
                showImageFour(urls, parent);
                break;
            default:
                showImageThreeMore(urls, parent);
                break;
        }
    }

    private void showImageOne(String[] urls, FlowLayout parent) {
        int imgSize = (int) ((width - DeviceUtils.dp2px(mContextRef.get(), 10)) / 1.5);
        for (String url : urls) {
            View view = mInflater.inflate(R.layout.zone_image_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            FlowLayout.LayoutParams params = new RecyclerView.LayoutParams(imgSize, imgSize);
//            AsyncImage.loadPhoto(mContextRef.get(), url, imageView);
            AsyncImage.loadNetPhotoWithListener(url, imageView, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                    int width = imgSize * loadedImage.getWidth()/loadedImage.getHeight();
                    RelativeLayout.LayoutParams viewParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    viewParams.width = width;
                    viewParams.height = imgSize;
                    view.setLayoutParams(viewParams);
                }
            });
            int padding = DeviceUtils.dp2px(mContextRef.get(), 5);
            view.setPadding(padding, padding, padding, padding);
            parent.addView(view, params);
        }
    }

    private void showImageTwo(String[] urls, FlowLayout parent) {

        int imgSize = (width - DeviceUtils.dp2px(mContextRef.get(), 10)) / 2;
        for (String url : urls) {
            View view = mInflater.inflate(R.layout.zone_image_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            FlowLayout.LayoutParams params = new RecyclerView.LayoutParams(imgSize, imgSize);
            AsyncImage.loadPhoto(mContextRef.get(), url, imageView);
            int padding = DeviceUtils.dp2px(mContextRef.get(), 5);
            view.setPadding(padding, padding, padding, padding);
            parent.addView(view, params);
        }

    }

    private void showImageFour(String[] urls, FlowLayout parent) {

        int imgSize = (width - DeviceUtils.dp2px(mContextRef.get(), 10)) / 2;
        for (String url : urls) {
            View view = mInflater.inflate(R.layout.zone_image_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            FlowLayout.LayoutParams params = new RecyclerView.LayoutParams(imgSize, imgSize);
            AsyncImage.loadPhoto(mContextRef.get(), url, imageView);
            int padding = DeviceUtils.dp2px(mContextRef.get(), 5);
            view.setPadding(padding, padding, padding, padding);
            parent.addView(view, params);
        }

    }

    private void showImageThreeMore(String[] urls, FlowLayout parent) {

        int imgSize = (width - DeviceUtils.dp2px(mContextRef.get(), 10)) / 3;
        for (String url : urls) {
            View view = mInflater.inflate(R.layout.zone_image_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            FlowLayout.LayoutParams params = new RecyclerView.LayoutParams(imgSize, imgSize);
            AsyncImage.loadPhoto(mContextRef.get(), url, imageView);
            int padding = DeviceUtils.dp2px(mContextRef.get(), 5);
            view.setPadding(padding, padding, padding, padding);
            parent.addView(view, params);
        }

    }

    private static class InnerLoadListener implements VideoThumbLoader.LoadListener {
        ViewGroup.LayoutParams rlParams;

        public InnerLoadListener(ViewGroup.LayoutParams rlParams) {
            this.rlParams = rlParams;
        }

        @Override
        public void onComplete(ImageView view, Bitmap b, String uri) {
            int height = view.getHeight();
            int width = height * b.getWidth() / b.getHeight();

            rlParams.width = width;
            rlParams.height = height;
            view.setLayoutParams(rlParams);
        }
    }

}
